'use strict';

const { Controller } = require('egg');

class HomeController extends Controller {
  // 增，删，改，查
  async addSlize() {
    const { ctx } = this;
    try {
      const res = await ctx.service.slize.addSlize(ctx.request.body);
      ctx.body = {
        code: 0,
        message: '数据添加成功',
        result: res,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        message: '数据添加失败',
        result: error,
      };
    }
  }

  async deleteSlize() {
    const { ctx } = this;
    try {
      const res = await ctx.service.slize.deleteSlize(ctx.params.id);
      ctx.body = {
        code: 0,
        message: '数据删除成功',
        result: res,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        message: '数据删除失败',
        result: error,
      };
    }
  }

  async updateSlize() {
    const { ctx } = this;
    try {
      const res = await ctx.service.slize.updateSlize(ctx.request.body);
      ctx.body = {
        code: 0,
        message: '数据更新成功',
        result: res,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        message: '数据更新失败',
        result: error,
      };
    }
  }

  async getSlize() {
    const { ctx } = this;
    try {
      const res = await ctx.service.slize.getSlize({ id: ctx.query.id });
      ctx.body = {
        code: 0,
        message: '数据查询成功',
        result: res,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        message: '数据查询失败',
        result: error,
      };
    }
  }

  async getSlizeList() {
    const { ctx } = this;
    try {
      const res = await ctx.service.slize.getSlizeList({
        pageSize: ctx.query.pageSize,
        pageNum: ctx.query.pageNum,
      });
      ctx.body = {
        code: 0,
        message: '数据查询成功',
        result: res,
      };
    } catch (error) {
      ctx.body = {
        code: 0,
        message: '数据查询失败',
        result: error,
      };
    }
  }
}

module.exports = HomeController;
